package com.cxrj.zhsq.userManage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.userManage.dao.MenuMapper;
import com.cxrj.zhsq.userManage.dao.RoleMapper;
import com.cxrj.zhsq.userManage.dao.UserMapper;
import com.cxrj.zhsq.userManage.entity.Menu;
import com.cxrj.zhsq.userManage.entity.Role;
import com.cxrj.zhsq.userManage.service.MenuService;

@Service
public class MenuServiceImpl implements MenuService {

	@Resource
	private MenuMapper menuMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	private RoleMapper roleMapper;

	@Override
	public List<Map<String, Object>> selectMenuTree(Map<String, Object> map, HttpServletRequest request) {
		List<Map<String, Object>> menus = menuMapper.selectMenusByParentId(map);
		String userId = (String) request.getSession().getAttribute("propertyUserId");
		String roleId = userMapper.selectRoleIdByuserId(userId);
		// List<Map<String, Object>> Role = roleMapper.selectRoleByIds(roleId);
		if (menus.size() == 0) {
			return null;
		} else {
			String temp = (String) roleMapper.selectRoleById(roleId).get("menuSet");
			for (Map<String, Object> m : menus) {
				String mmCode = (String) m.get("menuCode");
				int p = temp.indexOf(mmCode);
				if (p != -1) {//
					map.put("ztreeId", m.get("ztreeId"));
					List<Map<String, Object>> children = selectMenuTree(map, request);
					if (children != null) {
						Iterator<Map<String, Object>> it = children.iterator();
						while (it.hasNext()) {
							Map<String, Object> value = it.next();
							if (temp.indexOf((String) value.get("menuCode")) == -1) {//
								it.remove();
							}
							m.put("children", children);
						}
					}
				}

			}
			return menus;
		}
	}

	@Override
	public List<Map<String, Object>> selectMenuTreeAll(Map<String, Object> map, HttpServletRequest request) {
		List<Map<String, Object>> menus = menuMapper.selectMenusByParentId(map);
		if (menus.size() == 0) {
			return null;
		} else {
			for (Map<String, Object> m : menus) {
				map.put("ztreeId", m.get("ztreeId"));
				List<Map<String, Object>> children = selectMenuTree(map, request);
				if (children != null) {
					m.put("children", children);
				}
			}
			return menus;
		}

	}

	@Override
	public Map<String, Object> selectMenuCode(Map<String, Object> map) {
		Map<String, Object> Menu = menuMapper.selectMenuByCode(map);
		if (Menu != null) {
			map.put("result", 1);
		} else {
			map.put("result", 0);
		}
		return map;
	}

	@Override
	public void insertMenu(Map<String, Object> map) {
		map.put("menuId", UUID.randomUUID().toString());
		int a = (int) (Math.random() * (9999 - 1000 + 1)) + 1000;
		map.put("ztreeId", a);
		menuMapper.insertMenu(map);
	}

	@Override
	public void updateMenu(Map<String, Object> map) {
		menuMapper.updateMenu(map);
	}

	@Override
	public void deleteMenu(Map<String, Object> map) {
		List<Object> ids = new ArrayList<Object>();
		ids.add(map.get("ztreeId"));
		selectChildrenIds(map, ids);
		menuMapper.deleteByIds(ids);
	}

	private void selectChildrenIds(Map<String, Object> map, List<Object> ids) {

		List<Map<String, Object>> list = menuMapper.selectMenusByParentId(map);
		for (Map<String, Object> m : list) {
			ids.add(m.get("ztreeId"));
			map.put("ztreeId", m.get("ztreeId"));
			selectChildrenIds(map, ids);
		}
	}

	@Override
	public Map<String, Object> selectByRole(String roleId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Role role = roleMapper.selectByPrimaryKey(roleId);
		String[] codes = role.getMenuSet().split("\\+");
		Menu menu = menuMapper.selectByMenuCode("1");
		List<Menu> admins = menuMapper.selectByZtreePId(menu.getZtreeId());
		List<Menu> newAdmins = new ArrayList<Menu>();
		for (Menu menu2 : admins) {
			for (String string : codes) {
				if (menu2.getMenuCode().equals(string)) {
					newAdmins.add(menu2);
				}
			}
		}
		for (Menu menu2 : newAdmins) {
			List<Menu> subAdmins = new ArrayList<Menu>();
			List<Menu> list = menuMapper.selectByZtreePId(menu2.getZtreeId());
			for (Menu menu3 : list) {
				for (String string : codes) {
					if (menu3.getMenuCode().equals(string)) {
						subAdmins.add(menu3);
					}
				}
			}
			menu2.setMenus(subAdmins);
		}
		resultMap.put("list", newAdmins);
		return resultMap;
	}

}
